package se.mbaeumer.fxtodo.core.model;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import se.mbaeumer.fxtodo.core.util.constants.ValueConstants;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

public class Context {
	private IntegerProperty id = new SimpleIntegerProperty(this, "id");
	private StringProperty title = new SimpleStringProperty(this, "title");
	private StringProperty description = new SimpleStringProperty(this, "description");
	private Property<Date> created = new SimpleObjectProperty<Date>();
	private StringProperty createdString = new SimpleStringProperty(this, "createdString");
	private Property<TaskType> defaultTaskType = new SimpleObjectProperty<TaskType>();
	//private List<TaskType> allowedTaskTypes;

	public IntegerProperty idProperty(){return this.id;}
	public int getId(){return this.id.get();}
	public void setId(int id){this.id.set(id);}
	
	public StringProperty titleProperty(){return this.title;}
	public String getTitle(){return this.title.get();}
	public void setTitle(String title){this.title.set(title);}
	
	public StringProperty descriptionProperty(){return this.description;}
	public String getDescription(){return this.description.get();}
	public void setDescription(String description){this.description.set(description);}
	
	public Property<Date> createdProperty(){return this.created;}
	public Date getCreated(){return this.created.getValue();}
	public void setCreated(Date created){this.created.setValue(created);}
	
	public StringProperty createdStringProperty(){return this.createdString;}
	public String getCreatedString(){return this.createdString.get();}
	public void setCreatedString(String createdString){this.createdString.set(createdString);}
	
	public Property<TaskType> defaultTaskTypeProperty(){return this.defaultTaskType;}
	public TaskType getDefaultTaskType(){return this.defaultTaskType.getValue();}
	public void setDefaultTaskType(TaskType taskType){this.defaultTaskType.setValue(taskType);}
	
	public static class ContextBuilder{
		private int id;
		private String title;
		private String description;
		private Date created;
		private TaskType defaultTaskType;
		//private List<TaskType> allowedTaskTypes;
		
		public ContextBuilder id (int value){
			this.id = value;
			return this;
		}
		
		public ContextBuilder title(String value){
			this.title = value;
			return this;
		}
		
		public ContextBuilder description(String value){
			if (value == null){
				//TODO: Use constants instead of hard-coded strings
				value=ValueConstants.VALUE_N_A;
			}
			this.description = value;
			return this;
		}
		
		public ContextBuilder created(Date value){
			this.created = value;
			return this;
		}
		
		public ContextBuilder defaultTaskType(TaskType value){
			this.defaultTaskType = value;
			return this;
		}
		
		/*public ContextBuilder allowedTaskTypes (List<TaskType> value){
			this.allowedTaskTypes =value;
			return this;
		}*/
		
		public Context build(){
			return new Context(this);
		}
	}
	
	private Context(ContextBuilder contextBuilder){
		this.created.addListener(new ChangeListener<Date>(){

			@Override
			public void changed(ObservableValue<? extends Date> arg0,
					Date arg1, Date arg2) {
				// TODO Auto-generated method stub
				updateCreatedString(arg2);
			}
			
		});

		this.id.set(contextBuilder.id);
		this.title.set(contextBuilder.title);
		this.description.set(contextBuilder.description);
		this.created.setValue(contextBuilder.created);
		this.defaultTaskType.setValue(contextBuilder.defaultTaskType);
		//this.allowedTaskTypes = contextBuilder.allowedTaskTypes;
	}
	
	private void updateCreatedString(Date created){
		// TODO: Use constants instead of hard-coded strings
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		this.setCreatedString(df.format(created));
				
	}
}
